﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;
using HRProject.Code.DTO.Salary.Sale;

namespace HRProject.Code.DAO.Salary.Sale
{
    public class BasicSalaryDAO : BaseDAO
    {
        private static string FEATURE = "salesalary";
        private static string MODE = "basicsalary";

        public BasicSalaryDAO()
            : base ()
        {
        }

        public BLBasicSalaryTable Collect(BLJobTitle jobTitleFilter, Guid agentId, string fullName, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLBasicSalaryTable result = new BLBasicSalaryTable();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_CollectSaleBasicSalaryInfo", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAgentBasicSalary info = new BLAgentBasicSalary();
                info.Order = (int)data.Rows[i]["rownum"];
                info.Agent.Id = (Guid)data.Rows[i]["AgentId"];
                info.Agent.FullName = (string)data.Rows[i]["FullName"];
                info.Agent.Phone = (string)data.Rows[i]["Phone"];
                info.Agent.BankAccount = new BLBankAccount();
                info.Agent.BankAccount.AtmSerial = (string)data.Rows[i]["ATMSerial"];
                info.Agent.BankAccount.BankName = (string)data.Rows[i]["BankName"];
                //info.ContractDate = (DateTime)data.Rows[i]["ContractDate"];
                info.Salary.InfoNumberValue = (int)data.Rows[i]["BaseSalary"];
                info.MoneyLimit.InfoNumberValue = (int)data.Rows[i]["MoneyLimit"];
                info.SuperMarket.InfoNumberValue = (int)data.Rows[i]["MarketId"];
                info.SuperMarket.InfoStringValue = (string)data.Rows[i]["MarketName"];
                result.AgentBasicSalaryInfos.Add(info);
            }
            return result;
        }

        public int Update(BLAgentBasicSalary info, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@agentId", info.Agent.Id, ParameterDirection.Input));
            //condition.Add(new BaseCondition("@date", info.ContractDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoName", info.Salary.InfoName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoValue", info.Salary.InfoNumberValue, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", info.Salary.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", FEATURE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@mode", MODE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", info.Salary.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateAgentSalaryInfoWithoutDate", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;

            if (errorCode != 0)
            {
                return result;
            }

            condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", info.Agent.Id, ParameterDirection.Input));
            //condition.Add(new BaseCondition("@date", info.ContractDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoName", info.MoneyLimit.InfoName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoValue", info.MoneyLimit.InfoNumberValue, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", info.MoneyLimit.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", FEATURE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@mode", MODE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", info.MoneyLimit.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            result += update("usp_UpdateAgentSalaryInfoWithoutDate", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;

            if (errorCode != 0)
            {
                return result;
            }

            condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", info.Agent.Id, ParameterDirection.Input));
            //condition.Add(new BaseCondition("@date", info.ContractDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoName", info.SuperMarket.InfoName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@infoValue", info.SuperMarket.InfoNumberValue, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", info.SuperMarket.InfoStringValue, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", FEATURE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@mode", MODE, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", info.SuperMarket.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            result += update("usp_UpdateAgentSalaryInfoWithoutDate", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;

            if (errorCode != 0)
            {
                return result;
            }
            condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", info.Agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@phone", info.Agent.Phone, ParameterDirection.Input));
            condition.Add(new BaseCondition("@atmSerial", info.Agent.BankAccount.AtmSerial, ParameterDirection.Input));
            condition.Add(new BaseCondition("@bankName", info.Agent.BankAccount.BankName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", info.Agent.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            result += update("usp_UpdateAgentSaleSalaryInfo", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;

            return result;
        }
    }
}
